<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class FreeVouchers_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
	}

    /**
     * @param $voucher_id
     * @return mixed
     * 查询单条
     */
    public function get_free_vouchers_by_id($voucher_id){
        $query = $this->db
            ->select('*')
            ->get_where('free_vouchers', array('voucher_id'=>$voucher_id));
        return $query->row_array();
    }

    public function get_free_vouchers_by_uid($uid){
        $query = $this->db
            ->select('*')
            ->get_where('free_vouchers', array('user_id'=>$uid));
        return $query->row_array();
    }

    public function get_free_vouchers_by_uid_gid($uid,$good_id){
        $query = $this->db
            ->select('*')
            ->get_where('free_vouchers', array('user_id'=>$uid,'good_id'=>$good_id));
        return $query->row_array();
    }

    /**
     * @param $voucher_id
     * @param $data
     * @return bool
     * 修改商家信息
     */
    public function sava_free_vouchers($voucher_id,$data){

        $this->db->where('voucher_id',$voucher_id);
        $this->db->update('free_vouchers', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    /**
     * @param $data
     * @return bool
     * 新增商家
     */
    public function add_free_vouchers($data){

        $this->db->insert('free_vouchers',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有商家
     */
    public function get_all_free_vouchers(){
        $this->db->select('*');
        $this->db->from('free_vouchers');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }


    /**
     * 获取启用的规则
     */
    public function get_free_vouchers_by($uid,$status = 0){
            
            if($status == 2){
                $query = $this->db
                ->select('free_vouchers.*,shop_users.shop_title')
                ->from('free_vouchers')
                ->join('shop_users','shop_users.shop_id=free_vouchers.shop_id','LEFT')
                ->where('free_vouchers.user_id', $uid)
                ->where('free_vouchers.voucher_end_time <',time())
                ->where('free_vouchers.voucher_lock', 0)
                ->get();
            }else{
                $query = $this->db
                ->select('free_vouchers.*,shop_users.shop_title')
                ->from('free_vouchers')
                ->join('shop_users','shop_users.shop_id=free_vouchers.shop_id','LEFT')
                ->where('free_vouchers.user_id', $uid)
                ->where('free_vouchers.voucher_end_time >',time())
                ->where('free_vouchers.voucher_lock', $status)
                ->get();
            }
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除商家
     */
    public function delete_free_vouchers_by($where){
        $this->db->where($where)->delete('free_vouchers');
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

}
